package cn.com.thinker.core.config;

import cn.com.thinker.core.dao.jpa.support.ThinkerRepositoryFactoryBean;
import cn.com.thinker.core.nosql.redis.JsonRedisTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.http.HttpStatus;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.client.RestTemplate;

/**
 * Created by crazyHC on 2017/6/9.
 */
@Configuration
@EnableAsync
@EnableScheduling
@EnableCaching
@EnableAspectJAutoProxy(proxyTargetClass = true)
@EnableTransactionManagement(proxyTargetClass = true)
@ServletComponentScan(basePackages="cn.com.thinker")
@EntityScan(basePackages = "cn.com.thinker")
@EnableJpaRepositories(basePackages = {"cn.com.thinker"}, repositoryFactoryBeanClass = ThinkerRepositoryFactoryBean.class)
public class ThinCoreConfig {

    @Autowired
    private RestTemplateBuilder builder;

    // 使用RestTemplateBuilder来实例化RestTemplate对象，spring默认已经注入了RestTemplateBuilder实例
    @Bean
    @ConditionalOnMissingBean(name = "restTemplate")
    public RestTemplate restTemplate() {
        return builder.build();
    }

    @Bean
    public <T> RedisTemplate<String, T> jsonRedisTemplate(RedisConnectionFactory factory) {
        return new JsonRedisTemplate<String,T>(factory);
    }

    @Bean
    public RedisSerializer<Object> defaultSerializer(RedisConnectionFactory factory){
        return new GenericJackson2JsonRedisSerializer();
    }

    @Bean
    public CacheManager cacheManager(
            @SuppressWarnings("rawtypes") RedisTemplate jsonRedisTemplate) {
        return new RedisCacheManager(jsonRedisTemplate);
    }

//    @Bean @SuppressWarnings("SpringJavaAutowiringInspection")
//    public EmbeddedServletContainerCustomizer containerCustomizer(){
//
//        return (container -> {
//            ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "templates/error/404.html");
//            ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "templates/error/500.jsp");
//            ErrorPage errorThrowablePage = new ErrorPage(Throwable.class, "templates/error/500.jsp");
//            container.addErrorPages( errorThrowablePage,error404Page, error500Page);
//        });
//    }
}
